'''
Created on Jul 6, 2010

@author: v-grwang
'''

from urlparse import urlparse


class UrlPicker():
    IncludingList = []
    ExcludingList = []
    
    
    def __init__(self, includingList, excludingList):
        for url in includingList:
            try:
                self.IncludingList.append(self.NormalizeURL(url))                
            except:
                pass
                
                
        for url in excludingList:
            try:
                self.ExcludingList.append(self.NormalizeURL(url))                
            except:
                pass
    
    
    def NormalizeURL(self, url):
        if url[0:7].lower() != "http://" and url[0:8].lower() != "https://":
            url = "http://" + url 
        return urlparse(url)
    
    
    def ShouldFollow(self, url):
        url = self.NormalizeURL(url)
        
        if url.netloc == type(""):
            return False
        
        isIncluded = False
        for includePattern in self.IncludingList:
            isIncluded = self.IsSameDomain(url, includePattern)
            if isIncluded:
                break
                    
        
        isExcluded = False
        for excludePattern in self.ExcludingList:
            isExcluded = self.IsSameDomain(url, excludePattern)
            if isExcluded:
                return not isExcluded
                break
        
        return isIncluded
        
        
    
    
    def IsSameDomain(self, newURL, targateDomain):
        if newURL.netloc == targateDomain.netloc:
            if targateDomain.path == "/*"  or targateDomain.path == "":
                return True;            
            elif newURL.path.find(targateDomain.path) == 0:
                return True
            else:
                return False
        else:
            return False



if __name__ == "__main__":
    u = UrlPicker(["www.msn.com", "autos.msn.com"], ["http://autos.msn.com/research/savedinfo"])
    print u.ExcludingList
    print u.IncludingList
    print u.ShouldFollow("www.msn.com")
    print u.ShouldFollow("http://editorial.autos.msn.com/used-cars/default.aspx")
    print u.ShouldFollow("http://editorial.autos.msn.com/shows")
    print u.ShouldFollow("http://autos.msn.com/shows")
    print u.ShouldFollow("http://autos.msn.com/research/savedinfo/ModifyResearch.aspx")
    
    